<?php
/**
 * Create by wenyi
 * E-mail: wesley_prc@163.com
 * Description: data-collect-api
 * Date: 2024/3/3 14:14
 * Update: 2024/3/3 14:14
 * Remarks:公共基础业务类
 */

namespace app\api\business;
use think\Exception;

class CommonBussssss extends BaseBus
{
    private $model;
    private $className;
    // 初始化类
    public function __construct($busName){
        $name = mb_substr($busName, 0,-3); // 获取类名称不带bus字符
        $this->className = $name;
        $class = '\\app\\common\\model\\mysql\\'.$name; // 初始化数据库类
        $this->model =new $class();
        return $this->model;
    }

    /**
     * 根据条件查询
     * 示例:['id'=>1,'name'=>'测试']
     * @return array|false
     */
    public function getConditionalQuery($where)
    {
        try{
            $info = $this->model->getConditionalQuery($where);
            if(empty($info)){
                return false;
            }
            return $info->toArray();
        } catch(\Exception $e) {
            exception($e,$this->className.'.getConditionalQuery');
        }
    }

    /**
     * 根据条件查询多条信息
     * 示例:['id'=>1,'name'=>'测试']
     * @return array|false
     */
    public function getConditionalQueryAll($where)
    {
        try{
            $info = $this->model->getConditionalQueryAll($where);
            if(empty($info)){
                return false;
            }
            return $info->toArray();
        } catch(\Exception $e) {
            exception($e,$this->className.'.getConditionalQueryAll');
        }
    }

    /**
     * 根据条件查询多条信息分页
     * 示例:['id'=>1,'name'=>'测试']
     * @return array|false
     */
    public function getConditionalQueryAllPaging($where,$page,$limit)
    {
        try{
            $info = $this->model->getConditionalQueryAllPaging($where,$page,$limit);
            if(empty($info)){
                return false;
            }
            return $info->toArray();
        } catch(\Exception $e) {
            exception($e,$this->className.'.getConditionalQueryAll');
        }
    }

    /**
     * 获取多个数据
     * @return void
     */
    public function getMultipleData($where)
    {
        try{
            $res = $this->model->getMultipleData($where);
            if(empty($res)){
                return false;
            }
            return $res->toArray();
        } catch(\Exception $e) {
            exception($e,$this->className.'.getMultipleData');
        }
    }

    /**
     * 分页列表
     * @param array $data
     * @return array
     * @throws Exception
     */
    public function pagingListData(array $data=[])
    {
        try {
            if(empty($data) || !isset($data['page']) || !isset($data['limit'])){
                return false;
            }
            // 限定条件
            $where = array();
            if(!empty($data['id'])) {
                $where[]=['id', '=', $data['id']];
            }
            if(!empty($data['type'])) {
                $where[]=['type', '=', $data['type']];
            }
            if(!empty($data['name'])) {
                $where[]=['name', 'like', "%{$data['name']}%"];
            }
            // 需要显示的字段，默认空全部显示
            $displayField=[];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = [];
            // 排序
            $order = ["id"=>"desc"];
            // 分页
            $page = $data['page'];
            $limit =  $data['limit'];
            if($page==0 || empty($limit)){
                $page = 1;
            }
            if($data['limit'] ==0 || empty($data['limit']) || $data['limit']<0){
                $limit = 10;
            }
            // 查询数据
            $res = $this->model->pagingListDataSql($where,$displayField,$hideField,$order,$page,$limit);
            if(empty($res)){
                return array();
            }
            return $res->toArray();
        }catch (\Exception $e){
            exception($e,$this->className.'.pagingListData');
        }
    }

    /**
     * 全部数据列表
     * @param array $data
     * @return array
     * @throws Exception
     */
    public function listData(array $data=[])
    {
        try {
            // 限定条件
            $where = array();
            if(!empty($data['id'])) {
                $where[]=['id', '=', $data['id']];
            }
            if(!empty($data['type'])) {
                $where[]=['type', '=', $data['type']];
            }
            if(!empty($data['name'])) {
                $where[]=['name', 'like', "%{$data['name']}%"];
            }
            // 需要显示的字段，默认空全部显示
            $displayField=[];
            // 需要排除显示的字段，默认空全部显示,优先级大
            $hideField = [];
            // 排序
            $order = ["id"=>"desc"];
            // 查询数据
            $res = $this->model->listDataSql($where,$displayField,$hideField,$order);
            if(empty($res)){
                return array();
            }
            return $res->toArray();
        }catch (\Exception $e){
            exception($e,$this->className.'.listData');
        }
    }

    /**
     * 添加数据
     * @param $data // 添加的数据
     * @param $data
     * @throws Exception
     */
    public function addData($data){
        try {
            if (count($data) == count($data, 1)){
                if($this->model->save($data)){
                    return $this->model->id;
                }else{
                    return false;
                }
            }else {
                $res = $this->model->insertAll($data);
                if ($res) {
                    return $res;
                } else {
                    return false;
                }
            }
        }catch (\Exception $e){
            exception($e,$this->className.'.addData');
        }
    }

    /**
     * 更新信息
     * @param $data
     * @throws Exception
     */
    public function updateData($data,$where=[]){
        try {
            return $this->model->updateDataModel($data,$where)->toArray();
        }catch (\Exception $e){
            exception($e,$this->className.'.updateData');
        }
    }

    /**
     * 字段值自减
     * @param $id
     * @param $value
     * @return false|void
     */
    public function decrementing($id,$value=1)
    {
        try {
            if(empty($id)){
                return false;
            }
            return $this->model->decrementing($id,$value=1);
        }catch (\Exception $e){
            exception($e,$this->className.'.decrementing');
        }
    }

    /**
     * 根据主键删除信息
     * @param $id // 主键ID
     * @throws Exception
     */
    public function deleteData($id){
        try {
            if(empty($id)){
                return false;
            }
            return $this->model->where('id',$id)->delete($id);
        }catch (\Exception $e){
            exception($e,$this->className.'.deleteData');
        }
    }

    /**
     * 根据非主键条件删除信息
     * @param $ID
     * @return array|false|\josn|void
     * @throws Exception
     */
    public function conditionDeletion($where){
        try {
            if(empty($where)){
                return false;
            }
            // 删除信息
            return $this->model->where($where)->delete();
        }catch (\Exception $e){
            exception($e,$this->className.'.conditionDeletion');
        }
    }
    public function getOrderInfo($order)
    {
        try{
            $info = $this->model->getOrderInfo($order);
            if(empty($info)){
                return false;
            }
            return $info->toArray();
        } catch(\Exception $e) {
            exception($e,$this->className.'.getOrderInfo');
        }
    }
}